package com.kgc.service.impl;

import com.alibaba.fastjson.JSON;
import com.kgc.constants.Constants;
import com.kgc.dto.UsersDto;
import com.kgc.pojo.Users;
import com.kgc.mapper.UsersMapper;
import com.kgc.service.UsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.utils.UUIDUtils;
import com.kgc.utils.UrlUtils;
import com.kgc.utils.redis.RedisKeyUtils;
import com.kgc.utils.redis.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author zsy
 * @since 2021-08-04
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {


    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public UsersDto loginweChat(String access_token, String openid) {
         UsersDto usersDto = usersMapper.getUsersDto(openid);
         if (usersDto == null) {
             String url = "https://api.weixin.qq.com/sns/userinfo" +
                     "?access_token=" + access_token +
                     "&openid=" + openid;
             //获取信息
             String resultJson = UrlUtils.loadURL(url);
             //转为map
             Map<String, Object> map = JSON.parseObject(resultJson, Map.class);
             // 获取信息，并放入users对象
             if (map != null) {
                 Users users = new Users();
                 users.setOpenId(openid);
                 if (map.get("nickname") != null) {
                     users.setUserName(map.get("nickname").toString());
                 }
                 if (map.get("sex") != null) {
                     users.setSex(Integer.parseInt(map.get("sex").toString()));
                 }
                 if (map.get("headimgurl") != null) {
                     users.setFaceUrl(map.get("headimgurl").toString());
                 }
                 //将信息存储到数据库
                 usersMapper.insert(users);
                 usersDto = new UsersDto(users);
             }
         }

        setToken(usersDto);
        return usersDto;
    }

    @Override
    public UsersDto getLoginByToken(String token) {
        UsersDto usersDto;
        //判断redis中有没有token来获取信息
        String key = RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_TOKEN, token);
        String value = redisUtils.getValue(key);
        //如果有则返回usersdto信息
        if (!StringUtils.isBlank(value)){
            usersDto = JSON.parseObject(value, UsersDto.class);
            return usersDto;
        }else {
            return null;
        }
    }

    public void setToken(UsersDto usersDto){
        //设置token
        String  uuid = UUIDUtils.getUUID32();
        //存储到对象
        usersDto.setToken(uuid);
        //存到redis
         String key= RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_TOKEN, uuid);
        // 将对象转成json
        String usersJson = JSON.toJSONString(usersDto);
        // 过期时间 Constants.Duration.HALF_AN_HOUR_INT 半小时
        redisUtils.putValue(key,usersJson,Constants.Duration.HALF_AN_HOUR_INT);
    }
}
